<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false" %>
<%
    String id=request.getParameter("id");
    System.out.println("试卷id:"+id);
%>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加题目</title>
</head>
<body>
    <form action="/examproject/stu/addquestion" method="post">
    <h1 style="padding-left: 50%">添加试题</h1>
    <div style="margin-left: 50%">
    <label>题库分类:</label>
    <select id="sid" name="type" onchange="select()">
        <option value="所有试题">所有试题</option>
    </select>
     <input type="hidden" value="<%=id%>" name="id">
    <label>总分:</label>
     <h2>
         <input type="text" id="h2" name="question_score" readonly="readonly" value="0">
     </h2>
        <input type="text" style="display: none" id="question_ids" name="question_ids">
        <input type="submit" style="background-color: #00aced; width: 200px; height: 100px;font-size: 20px" value="发布">

    </div>
    <div id="xs" style="overflow:scroll; width:400px; height:400px;">
    </div>
    </form>
<script>
    //ajax请求班级
    var xmlhttp=new XMLHttpRequest();
    window.onload=function(){
        xmlhttp.open("GET","/examproject/stu/book",true);
        xmlhttp.send();
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState==4 && xmlhttp.status==200){

                var sz=JSON.parse(xmlhttp.responseText);
                var obj=document.querySelector("#sid");
                for(var i=0;i<sz.length;i++){
                    var op=new Option(sz[i].book_name,sz[i].book_name);
                    obj.appendChild(op);
                }
            }
        }
    }
    function select() {
        var div=document.createElement("div");
        var sel=document.getElementById("sid");
        xmlhttp.open("GET","/examproject/stu/question?name="+sel.value,true);
        xmlhttp.send();
        xmlhttp.onreadystatechange=function(){

            if(xmlhttp.readyState==4 && xmlhttp.status==200){
                var sz=JSON.parse(xmlhttp.responseText);
                var obj=document.querySelector("#xs");
                for(var i=0;i<sz.length;i++){
                    var label=document.createElement("label");
                    var check=document.createElement("input");
                    var h1=document.createElement("h1");
                    var txt=document.createTextNode(sz[i].question_name+"分数:"+sz[i].question_score);
                    check.type="checkbox";
                    check.name="question";
                    check.class="question";
                    check.id="question";
                    if(sz[i].question_type==1){
                        h1.innerHTML="单选";
                    }else{
                        h1.innerHTML="多选";
                    }
                    check.innerHTML=sz[i].question_name;
                    check.value=sz[i].question_name+"id:"+sz[i].question_id+"分数"+sz[i].question_score;
                    label.appendChild(h1);
                    label.appendChild(check);
                    label.appendChild(txt);
                    div.appendChild(label);
                }
            }

            if(div.innerHTML==null) {
                obj.appendChild(div)
            }else{
                var pObjs = obj.childNodes;
                for (var i = pObjs.length - 1; i >= 0; i--) { // 一定要倒序，正序是删不干净的，可自行尝试
                    obj.removeChild(pObjs[i]);
                }
                obj.appendChild(div)
            }
            var ids=new Array();
            var inp=document.querySelectorAll("#question");
            var h2=document.getElementById("h2");
            h2.value=0;
            var question_id=document.getElementById("question_ids");
           // alert(inp.length)
            for (var i=0;i<inp.length;i++) {
                inp[i].onchange=function () {
                    if(this.checked==true){
                        h2.value=parseInt(h2.value)+parseInt(this.value.substring(this.value.length-1));
                    //    alert("id:"+this.value.substring(this.value.indexOf(":")+1,this.value.indexOf("分")))
                        ids.push(this.value.substring(this.value.indexOf(":")+1,this.value.indexOf("分")))
                    //    alert("ids:"+ids);
                        question_id.value=ids;
                    }else{
                        h2.value=parseInt(h2.value)-parseInt(this.value.substring(this.value.length-1));
                        ids.pop(this.value.substring(this.value.indexOf(":")+1,this.value.indexOf("分")));
                    //    alert("ids:"+ids);
                        question_id.value=ids;
                    }
                }
            }

        }

    }

</script>
</body>
</html>